/**
* 一些通用方法的Module
*
*/
var _speech_define = (function ($) {

    var _speech_define = {};

    /** ================================【定义 function】========================================================== */
    _speech_define.fn = {};

    /**
     * function 语音合成
     */
    _speech_define.fn.speech = function (voiceName, pitch, rate, volume, chapterIndex, chapters, chapterNames, speechSelector) {

        const utterance = new SpeechSynthesisUtterance();

        // 配置语音属性（可选）
        $.each(speechSynthesis.getVoices(), function(i, voice) {
            if (voice.name == voiceName) {
                utterance.voice = voice;
                return false;
            }
        });
        utterance.pitch = pitch;
        utterance.rate = rate;
        utterance.volume = volume;
        utterance.text = (chapterIndex > 0 ? chapterNames[chapterIndex - 1] : '') + chapters[chapterIndex];

        // 标注读到哪一章
        if (chapterIndex == 0) {
            $(speechSelector).html('《' + chapters[0].split('\n')[0] + '》');
        } else  {
            $(speechSelector).html('《' + chapters[0].split('\n')[0] + '》' + ' ' + utterance.text.split('\n')[0]);
        }

        speechSynthesis.speak(utterance);

        utterance.addEventListener("end", function(event) {
            if (chapterIndex < chapters.length - 1) {
                _speech_define.fn.speech(voiceName, pitch, rate, volume, ++chapterIndex, chapters, chapterNames, speechSelector);
            } else {
                setTimeout(function () {
                    console.log('滚动结束！');
                });
                console.log('朗读结束！');
            }
        });

    };

    /**
     * function 语音合成
     */
    _speech_define.fn.speak = function (voiceName, pitch, rate, volume, text) {

        const utterance = new SpeechSynthesisUtterance();

        // 配置语音属性（可选）
        $.each(speechSynthesis.getVoices(), function(i, item) {

            if (item.name == voiceName) {
                utterance.voice = item;
                return false;
            }
        });
        utterance.pitch = pitch;
        utterance.rate = rate;
        utterance.volume = volume;
        utterance.text = text;

        speechSynthesis.speak(utterance);

        utterance.addEventListener("end", function(event) {
            console.log('朗读结束！');
        });
    };

    /**
     * function 暂停
     */
    _speech_define.fn.pause = function () {
        speechSynthesis.pause();

    };

    /**
     * function 继续
     */
    _speech_define.fn.resume = function () {
        speechSynthesis.resume();
    };

    /**
     * function 取消
     */
    _speech_define.fn.cancel = function () {
        speechSynthesis.cancel();
    };

    /**
     * function 设置voice
     */
    _speech_define.fn.voice = function (selector) {

        if ('speechSynthesis' in window) {
            $(selector).empty();
            $.each(speechSynthesis.getVoices(), function (i, voice) {
                $(selector).append($('<option>', {value: voice.name, text: voice.name}));
            });
        } else {
            console.error('浏览器不支持 speechSynthesis API');
        }
    };


    /** ========================================================================================== */
    return _speech_define;

})(jQuery);





